package com.tdam2011.grupo02.ui;

import java.util.LinkedList;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.compatibility.ActionBarListActivity;
import com.tdam2011.grupo02.connectivity.Connectivity;
import com.tdam2011.grupo02.data.DatabaseHelperSingleton;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ConnectivityLogListActivity extends ActionBarListActivity {

	ListView lv;
	ConnectivityAdapter adapter;
	LinkedList<Connectivity> connectivityData;
	OnItemClickListener connectivityItemClick;

	private static final String WIFI_CONNECTION = "WIFI";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connectivitylog);

		setTitle(R.string.txtvConnectivityLogName);

		lv = getListView();
		getConnectivityData();

		adapter = new ConnectivityAdapter(connectivityData);
		lv.setAdapter(adapter);
	}

	public LinkedList<Connectivity> getConnectivityData() {
		connectivityData = DatabaseHelperSingleton.getInstance(this)
				.getDatabaseHelper().getAllConnectivityInfo();

		return connectivityData;
	}

	public void updateList() {
		connectivityData = DatabaseHelperSingleton.getInstance(this)
				.getDatabaseHelper().getAllConnectivityInfo();
		adapter.setItems(connectivityData);
		adapter.notifyDataSetChanged();
	}

	public void deleteList() {
		DatabaseHelperSingleton.getInstance(this).getDatabaseHelper()
				.deleteAllConnectivityInfo();
		adapter.deleteAllItems();
		adapter.notifyDataSetChanged();
	}

	class ConnectivityAdapter extends BaseAdapter {

		LinkedList<Connectivity> connections;
		LayoutInflater inflater;
		final static int WIFI_CONNECTED = R.drawable.wifi_connected;
		final static int WIFI_DISCONNECTED = R.drawable.wifi_disconnected;
		final static int DATA_CONNECTED = R.drawable.data_connected;
		final static int DATA_DISCONNECTED = R.drawable.data_disconnected;

		public ConnectivityAdapter(LinkedList<Connectivity> connections) {
			this.connections = connections;
			inflater = LayoutInflater.from(ConnectivityLogListActivity.this);
		}

		@Override
		public int getCount() {
			return connections.size();
		}

		@Override
		public Object getItem(int position) {
			return connections.get(position);
		}

		@Override
		public long getItemId(int position) {
			return connections.get(position).get_id();
		}

		public void deleteAllItems() {
			connections = new LinkedList<Connectivity>();
		}

		public void setItems(LinkedList<Connectivity> connections) {
			this.connections = connections;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			Holder holder;
			final Connectivity info = (Connectivity) getItem(position);
			int img;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_connectivitylog,
						null);
				holder = new Holder();
				holder.img = (ImageView) convertView
						.findViewById(R.id.imgvConnectivity);
				holder.txtvConnectivity = (TextView) convertView
						.findViewById(R.id.txtvConnectivityLogConnection);
				holder.txtvStatus = (TextView) convertView
						.findViewById(R.id.txtvConnectivityLogStatus);
				holder.txtvFecha = (TextView) convertView
						.findViewById(R.id.txtvConnectivityLogFecha);
				holder.txtvHora = (TextView) convertView
						.findViewById(R.id.txtvConnectivityLogHora);

				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			if (WIFI_CONNECTION.equals(info.getConnection().trim())) {

				if (Connectivity.STATUS_CONNECTED.equals(info.getStatus()))
					img = WIFI_CONNECTED;
				else
					img = WIFI_DISCONNECTED;
			} else {
				if (Connectivity.STATUS_CONNECTED.equals(info.getStatus()))
					img = DATA_CONNECTED;
				else
					img = DATA_DISCONNECTED;
			}

			holder.txtvConnectivity.setText(info.getConnection());
			holder.txtvStatus.setText(info.getStatus());
			holder.txtvFecha.setText(info.getFecha());
			holder.txtvHora.setText(info.getHora());
			holder.img.setBackgroundResource(img);

			return convertView;

		}

		class Holder {
			CheckBox chkbx;
			ImageView img;
			TextView txtvConnectivity;
			TextView txtvStatus;
			TextView txtvFecha;
			TextView txtvHora;
		}
	}

	// OPTIONS MENU

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu_connectivity, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		case R.id.connectivityUpdate:
			updateList();
			getActionBarHelper().setRefreshActionItemState(true);
			getWindow().getDecorView().postDelayed(new Runnable() {
				@Override
				public void run() {
					getActionBarHelper().setRefreshActionItemState(false);
				}
			}, 1000);
			break;

		case R.id.connectivityDelete:
			deleteList();
			break;
		}

		return true;
	}

}
